package com.li.drug.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.li.drug.entity.Saleinfo;
import com.li.drug.service.SaleinfoService;
import com.li.drug.utils.ResultMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 销售记录相关controller
 */
@Controller
@RequestMapping("/saleinfo")
public class SaleinfoController {

    @Autowired
    SaleinfoService saleinfoService;

    /**
     * 转向销售记录页面
     * @return
     */
    @RequestMapping
    public String saleinfo(){
        return "saleinfo/saleinfo";
    }

    //分页查询销售记录列表
    @RequestMapping("/saleinfoQueryPage")
    @ResponseBody
    public Object saleinfoQueryPage(String param,
                                    @RequestParam(name = "page") int pageNum,
                                    @RequestParam(name = "limit") int pageSize){
        try {
            Page<Saleinfo> page = saleinfoService.selectSaleinfoPage(pageNum,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(page);

        }catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向销售记录新增页面
     * @return
     */
    @RequestMapping("/saleinfoPage")
    public String saleinfoPage(){
        return "saleinfo/saleinfoPage";
    }

    /**
     * 添加一个销售记录
     */
    @RequestMapping("/saleinfoAdd")
    @ResponseBody
    public Object saleinfo(Saleinfo saleinfo){
        saleinfo.setOperatetime(new Date());
        int i = saleinfoService.addSaleinfo(saleinfo);
        return ResultMapUtil.getHashMapSave(i);
    }

    /**
     * 转向销售记录编辑页面
     * @return
     */
    @RequestMapping("/saleinfoQueryById")
    public String saleinfoEditById(@RequestParam(name = "id") Integer id,
                                   Model model){
        Saleinfo saleinfo = saleinfoService.getSaleinfoById(id);
        model.addAttribute("saleinfo",saleinfo);
        return "saleinfo/saleinfoPage";
    }

    /**
     * 修改一个销售记录信息
     */
    @RequestMapping("/saleinfoEdit")
    @ResponseBody
    public Object saleinfoEdit(Saleinfo saleinfo){
        try {
            saleinfo.setOperatetime(new Date());
            int i = saleinfoService.editSaleinfo(saleinfo);
            return ResultMapUtil.getHashMapUpdate(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个销售记录信息
     */
    @RequestMapping("/saleinfoDelById")
    @ResponseBody
    public Object saleinfoDelById(@RequestParam(name = "id") Integer id){
        try {
            int i = saleinfoService.delSaleinfoById(id);
            return ResultMapUtil.getHashMapDel(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有销售记录列表
     */
    @RequestMapping("/saleinfoList")
    @ResponseBody
    public Object saleinfoList(){
        List<Saleinfo> list = saleinfoService.list();
        return list;
    }
}
